package org.custom.spring.boot.transaction.controller.propagation;

import org.custom.spring.boot.transaction.service.propagation.RequiresNewService;
import org.custom.spring.boot.useless.Result;
import org.springframework.context.annotation.Description;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 事务传播行为为REQUIRES_NEW
 *
 * @author LiKun
 * @date 2021/9/10 15:47
 */
@RestController
@RequestMapping("/requires/new")
@Description("a/A:不存在/存在事务的方法a；b/B:不存在/存在传播行为为REQUIRES_NEW的方法b；内/外部调用:不通过/通过Proxy(代理)调用")
public class RequiresNewController {
    @Resource
    private RequiresNewService requiresNewService;

    @Result("B会进行回滚，A不会进行回滚")
    @GetMapping("/catch/{data1}/{data2}")
    @Description("A外部调用B(抛出异常)并捕获B抛出的异常")
    public int withTransactionCatchRequiresNew(@PathVariable("data1") String data1, @PathVariable("data2") String data2) {
        return requiresNewService.withTransactionCatch(data1, data2);
    }

    @Result("B会进行回滚")
    @Description("a外部调用B(抛出异常)")
    @GetMapping("/without/{data1}/{data2}")
    public int withoutTransactionRequiresNew(@PathVariable("data1") String data1, @PathVariable("data2") String data2) {
        return requiresNewService.withoutTransaction(data1, data2);
    }

    @Result("A会进行回滚")
    @Description("A(抛出异常)外部调用B")
    @GetMapping("/exception/{data1}/{data2}")
    public int withTransactionExceptionRequiresNew(@PathVariable("data1") String data1, @PathVariable("data2") String data2) {
        return requiresNewService.withTransactionWithException(data1, data2);
    }
}
